#include <stdlib.h>
#include <stdio.h>
#include <math.h>

int largestRectangleArea(int *heights, int heightsSize) {
    int stack[heightsSize]; // 定义栈
    int top = -1;
    stack[++top] = 0;

    int max = -1;
    for (int i = 1; i < heightsSize; i++) // 遍历数组
    {
        if (heights[i] >= heights[stack[top]]) {
            stack[++top] = i;
        } else {
            while (top != -1 && heights[i] < heights[stack[top]]) {
                if (top - 1 == -1) {
                    max = fmax(max, i * heights[stack[top]]);
                } else {
                    max = fmax(max, (i - stack[top] + stack[top] - stack[top - 1] - 1) * heights[stack[top]]);
                }
                --top;
            }
            printf("%d\n", max);
            stack[++top] = i;
        }
    }
    while (top != -1) {
        if (top - 1 == -1) {
            max = fmax(max, (heightsSize) * heights[stack[top]]);
        } else {
            max = fmax(max, (heightsSize - 1 - stack[top - 1]) * heights[stack[top]]);
        }
        --top;
    }
    return max;
}

int main() {
    int test[] = {2, 1, 5, 6, 2, 3};
    int ret = largestRectangleArea(test, 6);
    printf("%d", ret);
}